package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.common.LineFunction;
import com.ruoyi.syrw.common.LineUtil;
import com.ruoyi.report_tg.domain.vo.GeneralCharDataVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdczbjlbsBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TdczbjlbsBsInfoVo;
import com.ruoyi.report_tg.domain.TdczbjlbsBsInfo;
import com.ruoyi.report_tg.mapper.TdczbjlbsBsInfoMapper;
import com.ruoyi.report_tg.service.ITdczbjlbsBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的承载比(CBR)记录三Service业务层处理
 *
 * @author zyb
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TdczbjlbsBsInfoServiceImpl implements ITdczbjlbsBsInfoService {

    private final TdczbjlbsBsInfoMapper baseMapper;

    /**
     * 查询土的承载比(CBR)记录三
     */
    @Override
    @FIllBasicInfo
//    @CoverRefCZB
    public TdczbjlbsBsInfoVo queryById(Long excelId){
        return compute(baseMapper.selectVoById(excelId));
    }

    /**
     * 查询土的承载比(CBR)记录三列表
     */
    @Override
    public TableDataInfo<TdczbjlbsBsInfoVo> queryPageList(TdczbjlbsBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdczbjlbsBsInfo> lqw = buildQueryWrapper(bo);
        Page<TdczbjlbsBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的承载比(CBR)记录三列表
     */
    @Override
    public List<TdczbjlbsBsInfoVo> queryList(TdczbjlbsBsInfoBo bo) {
        LambdaQueryWrapper<TdczbjlbsBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdczbjlbsBsInfo> buildQueryWrapper(TdczbjlbsBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdczbjlbsBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, TdczbjlbsBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, TdczbjlbsBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), TdczbjlbsBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), TdczbjlbsBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), TdczbjlbsBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), TdczbjlbsBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), TdczbjlbsBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), TdczbjlbsBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), TdczbjlbsBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, TdczbjlbsBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), TdczbjlbsBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), TdczbjlbsBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), TdczbjlbsBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), TdczbjlbsBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), TdczbjlbsBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), TdczbjlbsBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), TdczbjlbsBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(bo.getZuidgmd() != null, TdczbjlbsBsInfo::getZuidgmd, bo.getZuidgmd());
        lqw.eq(bo.getZuijhsl() != null, TdczbjlbsBsInfo::getZuijhsl, bo.getZuijhsl());
        lqw.eq(bo.getJiscs1() != null, TdczbjlbsBsInfo::getJiscs1, bo.getJiscs1());
        lqw.eq(bo.getJiscs2() != null, TdczbjlbsBsInfo::getJiscs2, bo.getJiscs2());
        lqw.eq(bo.getJiscs3() != null, TdczbjlbsBsInfo::getJiscs3, bo.getJiscs3());
        lqw.eq(bo.getYasd1() != null, TdczbjlbsBsInfo::getYasd1, bo.getYasd1());
        lqw.eq(bo.getYasd2() != null, TdczbjlbsBsInfo::getYasd2, bo.getYasd2());
        lqw.eq(bo.getYasd3() != null, TdczbjlbsBsInfo::getYasd3, bo.getYasd3());
        lqw.eq(bo.getCbr1() != null, TdczbjlbsBsInfo::getCbr1, bo.getCbr1());
        lqw.eq(bo.getCbr2() != null, TdczbjlbsBsInfo::getCbr2, bo.getCbr2());
        lqw.eq(bo.getCbr3() != null, TdczbjlbsBsInfo::getCbr3, bo.getCbr3());
        lqw.eq(bo.getYasd93() != null, TdczbjlbsBsInfo::getYasd93, bo.getYasd93());
        lqw.eq(bo.getYasd94() != null, TdczbjlbsBsInfo::getYasd94, bo.getYasd94());
        lqw.eq(bo.getYasd96() != null, TdczbjlbsBsInfo::getYasd96, bo.getYasd96());
        return lqw;
    }

    /**
     * 新增土的承载比(CBR)记录三
     */
    @Override
    public Boolean insertByBo(TdczbjlbsBsInfoBo bo) {
        TdczbjlbsBsInfo add = BeanUtil.toBean(bo, TdczbjlbsBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改土的承载比(CBR)记录三
     */
    @Override
    public Boolean updateByBo(TdczbjlbsBsInfoBo bo) {
        TdczbjlbsBsInfo update = BeanUtil.toBean(bo, TdczbjlbsBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdczbjlbsBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的承载比(CBR)记录三
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TdczbjlbsBsInfoVo compute(TdczbjlbsBsInfoVo vo) {
        // 1. 参数判断 如果有任何一个为空,则 直接原样返回
        if (BigDecimalUtil.isAnyOneEmpty(vo.getYasd1(), vo.getYasd2(), vo.getYasd3(), vo.getCbr1(),
            vo.getCbr2(), vo.getCbr3())) {
            return vo;
        }
        // 2 计算
        double[][] data = new double[3][2];
        data[0][0] = vo.getYasd1().doubleValue();
        data[0][1] = vo.getCbr1().doubleValue();
        data[1][0] = vo.getYasd2().doubleValue();
        data[1][1] = vo.getCbr2().doubleValue();
        data[2][0] = vo.getYasd3().doubleValue();
        data[2][1] = vo.getCbr3().doubleValue();
        LineFunction lineFunction = LineUtil.getGeneralLine(data);
        vo.setYasd93(BigDecimalUtil.defaultValue(lineFunction.getY(93), 2));
        vo.setYasd94(BigDecimalUtil.defaultValue(lineFunction.getY(94), 2));
        vo.setYasd96(BigDecimalUtil.defaultValue(lineFunction.getY(96), 2));
        // 3 画图的数据
        // 3.1 参与画图 不展示 不需要辅助线的点 100
        List<GeneralCharDataVo> charData = generateData(lineFunction, vo.getYasd1().doubleValue() - 1,
            vo.getYasd3().doubleValue() + 1);
        // 3.2 不参与画图 展示 不需要辅助线的点 010
        charData.add(new GeneralCharDataVo(new double[]{vo.getYasd1().doubleValue(), vo.getCbr1().doubleValue()}, "010"));
        charData.add(new GeneralCharDataVo(new double[]{vo.getYasd2().doubleValue(), vo.getCbr2().doubleValue()}, "010"));
        charData.add(new GeneralCharDataVo(new double[]{vo.getYasd3().doubleValue(), vo.getCbr3().doubleValue()}, "010"));
        // 3.3 不参与画图 展示 需要辅助线 011
        charData.add(new GeneralCharDataVo(new double[]{93, BigDecimalUtil.defaultValue(lineFunction.getY(93),2).doubleValue()}, "011"));
        charData.add(new GeneralCharDataVo(new double[]{94, BigDecimalUtil.defaultValue(lineFunction.getY(94),2).doubleValue()}, "011"));
        charData.add(new GeneralCharDataVo(new double[]{96, BigDecimalUtil.defaultValue(lineFunction.getY(96),2).doubleValue()}, "011"));
        vo.setChartData(charData);
        return vo;
    }

    private List<GeneralCharDataVo> generateData(LineFunction lineFunction, double start, double end) {
        List<GeneralCharDataVo> charData = new ArrayList<>(20);
        charData.add(new GeneralCharDataVo(new double[]{start, lineFunction.getY(start)}, "100"));
        charData.add(new GeneralCharDataVo(new double[]{end, lineFunction.getY(end)}, "100"));
        charData.add(new GeneralCharDataVo(new double[]{100d, lineFunction.getY(100d)}, "100"));
        return charData;
    }

}
